package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.PlayRecord;
import com.elinkway.infinitemovies.listener.OnItemListener;
import com.elinkway.infinitemovies.view.EqualRatioImageView;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by zhaopeipei on 2016/11/23.
 */
public class HistoryGalleryAdapter extends RecyclerView.Adapter<HistoryGalleryAdapter.MyViewHolder> {

    private static final String TAG = "history";

    private List<PlayRecord> mHistory;

    private Context mContext;

    private LayoutInflater mInflater;

    private OnItemListener mListener;

    public HistoryGalleryAdapter(Context context, List<PlayRecord> datats) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mHistory = datats;
    }

    public void setDatas(List<PlayRecord> datats) {
        mHistory = datats;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.my_poster_layout,
                parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        view.setOnClickListener(holder);
        return holder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final MyViewHolder viewHolder, final int i) {
        PlayRecord history;
        if (mHistory != null) {
            history = mHistory.get(i);
            if (history != null) {
                viewHolder.title.setText(history.getDisplayName());
                if (!TextUtils.isEmpty(history.getPoster())) {
                    ImageLoader.getInstance().displayImage(history.getPoster(), viewHolder.poster);
                } else {
                    viewHolder.poster.setImageResource(R.drawable.ic_launcher);
                }
            }
        }
    }

    public void setOnItemClickListnener(OnItemListener listnener) {
        mListener = listnener;
    }


    @Override
    public int getItemCount()
    {
        if (mHistory != null) {
            if (mHistory.size() < 20) {
                return mHistory.size();
            } else {
                return 20;
            }
        }
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        EqualRatioImageView poster;
        TextView title;
        public MyViewHolder(View view) {
            super(view);
            title = (TextView)view.findViewById(R.id.grid_item_title);
            poster = (EqualRatioImageView) view.findViewById(R.id.grid_item_poster);
        }

        /**
         * 点击监听
         */
        @Override
        public void onClick(View v) {
            if(mListener != null){
                mListener.OnItemClick(v, getPosition(), TAG);
            }
        }
    }
}



